package com.gxwgy.wg2404.controller;

import com.gxwgy.wg2404.util.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author 姚相国
 * @version 1.0
 * @className DeleteEvent
 * @date 2024/12/18 03:40
 */
@WebServlet({"/organizer/deleteEvent"})
public class DeleteEvent extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断是否获取到了session
        HttpSession session = request.getSession(false);
        // 如果有session，就表示已登录，可以执行相关操作
        if (session.getAttribute("username") == null) {
            request.getRequestDispatcher("/index.jsp").forward(request,response);
        }

        // 获取要删除的ID
        String eventsId = request.getParameter("id");
        String role = (String) session.getAttribute("role");

        // 连接数据库删除
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        try {
            conn = DBUtil.getConnection();
            // 开启事务，关闭自动提交事务
            conn.setAutoCommit(false);
            String sql = "delete from events where id = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, eventsId);
            count = ps.executeUpdate();
            // 事务提交
            conn.commit();
        } catch (SQLException e) {
            // 事务回滚
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.close(conn, ps, null);
        }

        // 判断成功或失败，进行页面的跳转
        if (count >= 1) {
            //删除成功
            // 获取当前用户的角色或其他标识（例如，管理员角色或主办方角色）
            // 根据角色或其他条件执行不同的重定向
            if ("organizer".equals(role)) {
                response.setContentType("text/html; charset=UTF-8");
                response.getWriter().println("<script>alert('删除成功！返回活动列表页面'); location.href='" + request.getContextPath() + "/organizer/dashboard';</script>");
            } else if ("admin".equals(role)) {
                response.setContentType("text/html; charset=UTF-8");
                response.getWriter().println("<script>alert('删除成功！返回管理面板'); location.href='" + request.getContextPath() + "/admin/dashboard';</script>");
            } else {
                // 默认的重定向逻辑，或者处理其他角色
                response.setContentType("text/html; charset=UTF-8");
                response.getWriter().println("<script>alert('删除成功！'); location.href='" + request.getContextPath() + "/default/dashboard';</script>");
            }

        } else {
            // 删除失败
            response.setContentType("text/html; charset=UTF-8");
            response.getWriter().println("<script>alert('删除失败！可能该活动不存在或您无权限删除'); location.href='" + request.getContextPath() + "/organizer/dashboard';</script>");
        }
    }
}
